server, where it can be used on reboot and migrate as well as create.
Ballooning bugs may still exist (it is unclear) but this is certainly a good
step in the right direction.
Ignore the memory and maxmem entries in the store for dom0 when restarting.
This means that the value in use at the time becomes the configured target.
This should fix the bug whereby dom0_mem settings on the command line are
being overridden by (older) entries in the store.
Signed-off-by: Ewan Mellor <ewan@xensource.com>
import xen.lowlevel.xc
+import balloon
from XendError import XendError
from XendLogging import log
raise XendError(
"not a valid guest state file: pfn count out of range")
+ balloon.free(xc.pages_to_kib(nr_pfns))
+
cmd = map(str, [xen.util.auxbin.pathTo(XC_RESTORE),
xc.handle(), fd, dominfo.getDomid(), nr_pfns,
store_port, console_port])
if line == "":
break
else:
- log.error('%s', line)
+ log.error('%s', line.strip())
from xen.util import asserts
from xen.util.blkif import blkdev_uname_to_file
+import balloon
import image
import sxp
import uuid
'Uuid in store does not match uuid for existing domain %d: '
'%s != %s' % (domid, uuid2_str, xeninfo['uuid']))
- vm = XendDomainInfo(xeninfo, domid, dompath, True)
+ vm = XendDomainInfo(xeninfo, domid, dompath, True, priv)
except Exception, exn:
if priv:
log.warn(str(exn))
- vm = XendDomainInfo(xeninfo, domid, dompath, True)
+ vm = XendDomainInfo(xeninfo, domid, dompath, True, priv)
vm.removeDom()
vm.removeVm()
vm.storeVmDetails()
class XendDomainInfo:
- def __init__(self, info, domid = None, dompath = None, augment = False):
+ def __init__(self, info, domid = None, dompath = None, augment = False,
+ priv = False):
self.info = info
self.dompath = dompath
if augment:
- self.augmentInfo()
+ self.augmentInfo(priv)
self.validateInfo()
return 1
- def augmentInfo(self):
+ def augmentInfo(self, priv):
"""Augment self.info, as given to us through {@link #recreate}, with
values taken from the store. This recovers those values known to xend
but not to the hypervisor.
if not self.infoIsSet(name) and val is not None:
self.info[name] = val
- map(lambda x, y: useIfNeeded(x[0], y), VM_STORE_ENTRIES,
- self.readVMDetails(VM_STORE_ENTRIES))
+ if priv:
+ entries = VM_STORE_ENTRIES[:]
+ entries.remove(('memory', int))
+ entries.remove(('maxmem', int))
+ else:
+ entries = VM_STORE_ENTRIES
+
+ map(lambda x, y: useIfNeeded(x[0], y), entries,
+ self.readVMDetails(entries))
device = []
for c in controllerClasses:
"""Set the memory target of this domain.
@param target In MiB.
"""
+ log.debug("Setting memory target of domain %s (%d) to %d MiB.",
+ self.info['name'], self.domid, target)
+
self.info['memory'] = target
self.storeVm("memory", target)
self.storeDom("memory/target", target << 10)
xc.domain_setcpuweight(self.domid, self.info['cpu_weight'])
m = self.image.getDomainMemory(self.info['memory'] * 1024)
+ balloon.free(m)
xc.domain_setmaxmem(self.domid, m)
xc.domain_memory_increase_reservation(self.domid, m, 0, 0)
--- /dev/null
+#===========================================================================
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of version 2.1 of the GNU Lesser General Public
+# License as published by the Free Software Foundation.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#============================================================================
+# Copyright (C) 2004, 2005 Mike Wray <mike.wray@hp.com>
+# Copyright (C) 2005 XenSource Ltd
+#============================================================================
+
+
+import time
+
+import xen.lowlevel.xc
+
+import XendDomain
+import XendRoot
+from XendLogging import log
+from XendError import VmError
+
+
+PROC_XEN_BALLOON = "/proc/xen/balloon"
+BALLOON_OUT_SLACK = 1 # MiB. We need this because the physinfo details are
+ # rounded.
+
+
+def free(required):
+ """Balloon out memory from the privileged domain so that there is the
+ specified required amount (in KiB) free.
+ """
+
+ xc = xen.lowlevel.xc.xc()
+ xroot = XendRoot.instance()
+
+ try:
+ free_mem = xc.physinfo()['free_memory']
+ need_mem = (required + 1023) / 1024 + BALLOON_OUT_SLACK
+
+ log.debug("Balloon: free %d; need %d.", free_mem, need_mem)
+
+ if free_mem >= need_mem:
+ return
+
+ dom0_min_mem = xroot.get_dom0_min_mem()
+ if dom0_min_mem == 0:
+ raise VmError('Not enough free memory and dom0_min_mem is 0.')
+
+ dom0_alloc = _get_dom0_alloc()
+ dom0_new_alloc = dom0_alloc - (need_mem - free_mem)
+ if dom0_new_alloc < dom0_min_mem:
+ raise VmError(
+ ('I need %d MiB, but dom0_min_mem is %d and shrinking to '
+ '%d MiB would leave only %d MiB free.') %
+ (need_mem, dom0_min_mem, dom0_min_mem,
+ free_mem + (dom0_alloc - dom0_min_mem)))
+
+ dom0 = XendDomain.instance().privilegedDomain()
+ dom0.setMemoryTarget(dom0_new_alloc)
+
+ timeout = 20 # 2 sec
+ while timeout > 0:
+ time.sleep(0.1)
+
+ free_mem = xc.physinfo()['free_memory']
+ if free_mem >= need_mem:
+ return
+
+ timeout -= 1
+
+ raise VmError('The privileged domain did not balloon!')
+ finally:
+ del xc
+
+
+def _get_dom0_alloc():
+ """Return current allocation memory of dom0 (in MiB). Return 0 on error"""
+
+ f = file(PROC_XEN_BALLOON, 'r')
+ try:
+ line = f.readline()
+ for x in line.split():
+ for n in x:
+ if not n.isdigit():
+ break
+ else:
+ return int(x) / 1024
+ return 0
+ finally:
+ f.close()
opts.info("Started domain %s" % (dom))
return int(sxp.child_value(dominfo, 'domid'))
-def get_dom0_alloc():
- """Return current allocation memory of dom0 (in MB). Return 0 on error"""
- PROC_XEN_BALLOON = "/proc/xen/balloon"
-
- f = open(PROC_XEN_BALLOON, "r")
- line = f.readline()
- for x in line.split():
- for n in x:
- if not n.isdigit():
- break
- else:
- f.close()
- return int(x)/1024
- f.close()
- return 0
-
-def balloon_out(dom0_min_mem, opts):
- """Balloon out memory from dom0 if necessary"""
- SLACK = 4
- timeout = 20 # 2s
- ret = 1
-
- xc = xen.lowlevel.xc.xc()
- free_mem = xc.physinfo()['free_pages'] / 256
- domU_need_mem = opts.vals.memory + SLACK
-
- # we already have enough free memory, return success
- if free_mem >= domU_need_mem:
- del xc
- return 0
-
- dom0_cur_alloc = get_dom0_alloc()
- dom0_new_alloc = dom0_cur_alloc - (domU_need_mem - free_mem)
- if dom0_new_alloc < dom0_min_mem:
- dom0_new_alloc = dom0_min_mem
-
- server.xend_domain_mem_target_set(0, dom0_new_alloc)
-
- while timeout > 0:
- time.sleep(0.1) # sleep 100ms
-
- free_mem = xc.physinfo()['free_pages'] / 256
- if free_mem >= domU_need_mem:
- ret = 0
- break
- timeout -= 1
-
- del xc
- return ret
-
-
def parseCommandLine(argv):
gopts.reset()
args = gopts.parse(argv)
if opts.vals.dryrun:
PrettyPrint.prettyprint(config)
else:
- from xen.xend import XendRoot
-
- xroot = XendRoot.instance()
-
- dom0_min_mem = xroot.get_dom0_min_mem()
- if dom0_min_mem != 0:
- if balloon_out(dom0_min_mem, opts):
- err("cannot allocate enough memory for domain")
-
dom = make_domain(opts, config)
if opts.vals.console_autoconnect:
console.execConsole(dom)